package com.zhihuiweijian.demo.controller;

import com.zhihuiweijian.demo.model.RolePermission;
import com.zhihuiweijian.demo.model.User;
import com.zhihuiweijian.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
@Api(tags={"用户操作接口"})
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping(value="/add")
    public Map<String, Object> add(User user) {
        Map<String, Object> map = new HashMap();
        try {
            userService.insert(user);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }


    @GetMapping(value="/delete")
    public Map<String, Object> delete(Long id) {
        Map<String, Object> map = new HashMap();
        try {
            userService.deleteByPrimaryKey(id);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }


    @GetMapping(value="/update")
    public Map<String, Object> update(User user) {
        Map<String, Object> map = new HashMap();
        try {
            userService.updateByPrimaryKey(user);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @GetMapping(value="/findAll")
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap();
        try {
            map.put("data",userService.findAll());
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }
    @GetMapping(value="/findByName")
    public Map<String, Object> findByName(String name) {
        Map<String, Object> map = new HashMap();
        try {
            map.put("data",userService.findByUsername(name));
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }



    @GetMapping(value="/findById")
    public Map<String, Object> findById(Long id) {
        Map<String, Object> map = new HashMap();
        try {
            map.put("data",userService.findByPrimaryKey(id));
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

}
